<template>
  <el-upload v-model:file-list="fileList" class="upload" :action="action" :show-file-list="false" :headers="headers"
    :data="data" :accept="accept" :on-success="handleSuccess" :limit="1">
    <el-button type="primary">导入</el-button>
  </el-upload>
</template>
<script setup>
import { ref } from "vue"
import { useUserStore } from "@/stores/user.ts"

const userStore = useUserStore()

const headers = ref({
  Token: userStore.vToken
})

const props = defineProps({
  accept: {
    type: String,
    default: '.xlsx'
  },
  data: {
    type: Object,
    default: () => ({})
  },
  action: {
    type: String,
    required: true
  }
})
const fileList = ref()

const emit = defineEmits(['success'])

const handleSuccess = (response) => {
  if (response.code == 200) {
    ElMessage.success(response.msg || '导入成功！')
    emit('success', response)
    return
  }
  ElMessage.error(response.msg || '导入失败！')
}

</script>
<style lang="scss" scoped>
.upload {
  margin: 0 10px;
}
</style>
